package com.mt.fpb.controller;

import com.alibaba.fastjson.JSONObject;
import com.mt.fpb.common.util.DateUtil;
import com.mt.fpb.mapper.TAirMonitorRecordMapper;
import com.mt.fpb.mapper.TWarningMapper;
import com.mt.fpb.mapper.TWaterMonitorRecordMapper;
import com.mt.fpb.model.TAirMonitorRecord;
import com.mt.fpb.model.TWarning;
import com.mt.fpb.model.TWaterMonitorRecord;
import com.mt.fpb.model.dto.BaseQueryParams;
import com.mt.fpb.model.vo.CommonResult;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 环境探测
 */
@RestController
@RequestMapping("/TAirMonitor")
public class TAirMonitorController {
    @Resource
    private TAirMonitorRecordMapper tAirMonitorRecordMapper;

    @Resource
    private TWaterMonitorRecordMapper tWaterMonitorRecordMapper;

    @Resource
    private TWarningMapper tWarningMapper;

    /**
     * 今日空气平均数据 ZFG
     * 左上 2021年6月12日19点52分
     */
    @GetMapping("getAvgData")
    public CommonResult getAvgData(BaseQueryParams queryParams) {
        JSONObject json = new JSONObject();
        // 获取今日空气平均数据
        TAirMonitorRecord avgData = tAirMonitorRecordMapper.getAvgData(queryParams.getMineId());
        // pm2.5
        json.put("PM2", !StringUtils.isEmpty(avgData) ? avgData.getPm25() : 0);
        //PM10
        json.put("PM10", !StringUtils.isEmpty(avgData) ? avgData.getPm10() : 0);
        // TSP
        json.put("TSP", !StringUtils.isEmpty(avgData) ? avgData.getTsp() : 0);
        //温度
        json.put("temperature", !StringUtils.isEmpty(avgData) ? avgData.getTemperature() : 0);
        return CommonResult.success(json);
    }

    /**
     * 监测数据预警
     * 左中 2021年6月13日
     */
    @GetMapping("getWarningData")
    public CommonResult getWarningData(BaseQueryParams queryParams) {
        JSONObject json = new JSONObject();
        List<Map> warningData = tWarningMapper.getWarningData(queryParams.getMineId());

        return CommonResult.success(warningData);
    }

    /**
     * 今日排污数据 ZFG
     * 左下 2021年6月13日
     */
    @GetMapping("getWaterData")
    public CommonResult getWaterData(BaseQueryParams queryParams) {
        JSONObject json = new JSONObject();
        // 获取今日排污数据
        TWaterMonitorRecord waterData = tWaterMonitorRecordMapper.getWaterData(queryParams.getMineId());
        // 污水
        json.put("waste", !StringUtils.isEmpty(waterData.getWasteWater()) ? waterData.getWasteWater() : 0);
        //ph值
        json.put("ph", !StringUtils.isEmpty(waterData.getPh()) ? waterData.getPh() : 0);
        // 化学需氧量
        json.put("chemistry", !StringUtils.isEmpty(waterData.getChemicalOxygenDemand()) ? waterData.getChemicalOxygenDemand() : 0);
        // 总氮
        json.put("total", !StringUtils.isEmpty(waterData.getTotalNitrogen()) ? waterData.getTotalNitrogen() : 0);
        // 氨氮
        json.put("ammonia", !StringUtils.isEmpty(waterData.getAmmoniaNitrogen()) ? waterData.getAmmoniaNitrogen() : 0);
        // 总磷
        json.put("toPhosphorus", !StringUtils.isEmpty(waterData.getTotalPhosphorus()) ? waterData.getTotalPhosphorus() : 0);
        return CommonResult.success(json);
    }

    /**
     * 实时监测数据 ZFG
     * 右上 2021年6月13日
     */
    @GetMapping("getMonitData")
    public CommonResult getMonitData(BaseQueryParams queryParams) {
        JSONObject json = new JSONObject();
        String inNoise = tAirMonitorRecordMapper.getInNoise(queryParams.getMineId());
        String outNoise = tAirMonitorRecordMapper.getOutNoise(queryParams.getMineId());
        String tspData = tAirMonitorRecordMapper.getTspData(queryParams.getMineId());
        String suspendData = tWaterMonitorRecordMapper.getSuspendData(queryParams.getMineId());

        // 场内噪音
        json.put("InNoise", !StringUtils.isEmpty(inNoise) ? inNoise : 0);
        // 场外噪音
        json.put("OutNoise", !StringUtils.isEmpty(outNoise) ? outNoise : 0);
        // TSP 数据
        json.put("TSP", !StringUtils.isEmpty(tspData) ? tspData : 0);
        // 悬浮物数据
        json.put("Suspend", !StringUtils.isEmpty(suspendData) ? suspendData : 0);

        return CommonResult.success(json);
    }

    /**
     * 今日实时数据折线图LT
     * 中下
     * 2021年6月15日14:26:05
     *
     * @param queryParams
     * @return
     */
    @GetMapping("getFactDataDays")
    public CommonResult getFactDataDays(BaseQueryParams queryParams) {
        //TSP总数
        List<String> TSP = new LinkedList<>();
        //噪音总数
        List<String> noise = new LinkedList<>();
        String[] strArray = {" 00:00:00", " 02:00:00", " 04:00:00", " 06:00:00", " 08:00:00", " 10:00:00", " 12:00:00", " 14:00:00", " 16:00:00", " 18:00:00", " 20:00:00", " 22:00:00", " 23:00:00"};
        for (String str : strArray) {
            //获取截止到当前时间的噪音和TSP数据
            TAirMonitorRecord tAirMonitorRecord = tAirMonitorRecordMapper.
                    getFactDataDays(DateUtil.getCurrentDate("yyyy-MM-dd") + str, queryParams.getMineId());
            //添加TSP值
            TSP.add(tAirMonitorRecord.getTsp());
            //添加噪音值
            noise.add(tAirMonitorRecord.getNoise());
        }
        JSONObject json = new JSONObject();
        json.put("TSP", TSP);
        json.put("noise", noise);
        return CommonResult.success(json);
    }
}
